//
//  HeadLineViewController.m
//  YouWen
//
//  Created by 刘永和 on 15/3/9.
//  Copyright (c) 2015年 刘永和. All rights reserved.
//

#import "HeadLineViewController.h"


@interface HeadLineViewController ()

@end

@implementation HeadLineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    self.allHeadLineArray = [NSMutableArray array];
    
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - 64)] ;
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    [self.view addSubview:self.myTableView];
  


    
    
    //标记上拉操作还是下拉操作，yes为上拉
    self.isUpLoading = NO;
    
    // 2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];
    
    self.myPage = 1;

    
}
    
#pragma mark --下拉刷新
- (void)addHeader
{

    __block HeadLineViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        vc.myPage = 1;
        vc.isUpLoading = NO;//标记为下拉操作
        [vc GetPersonList:0];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myTableView headerBeginRefreshing];
}
#pragma mark --上拉加载更多
- (void)addFooter
{
    __block HeadLineViewController *vc = self;
    
    
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
       
        vc.myPage++;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc GetPersonList:vc.myPage];//请求更多数据
        
    }];

    
}
#pragma mark --使用AFN进行数据请求
-(void)GetPersonList:(NSInteger)nextCursor
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    //****参数cursor的值: 第一页默认为0，下一页就是请求下来数据中next_cursor的值
    //****参数count 的值: 是指定一页显示多少条数据
   
    NSString *url_string = [NSString stringWithFormat:@"http://www.tingwen.me/akcms_category.php?id=1&page=%ld",self.myPage];
    

    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [netWorkManager stopMonitoring];
        
        
        
//        NSDictionary *allDic = responseObject;
        NSMutableArray *allArr = responseObject;
//        NSLog(@"使用AFN进行get请求 ===  %@",allArr);
        
//        //记录下一页
//        self.myPage++;
//        NSLog(@"nextCursor == %ld",self.myPage);
        
        if (self.isUpLoading == NO) {
            //说明是下拉刷新，就要清空数组中的数据，保持数组中存放的是最新的数据
            [self.allHeadLineArray removeAllObjects];
        }
        
        
//        NSArray *array = [allDic objectForKey:@"users"];
        for (NSDictionary *dic  in allArr) {
            
            HeadLine *headLine = [[HeadLine alloc] init];
//            person.personName = [dic objectForKey:@"name"];
//            headLine.title = [dic objectForKey:@"title"];
            [headLine setValuesForKeysWithDictionary:dic];
            [self.allHeadLineArray addObject:headLine];
            
            
            
        }
       
        
        [self.myTableView footerEndRefreshing];//结束底部刷新效果
        [self.myTableView headerEndRefreshing];//结束头部刷新效果
        [self.myTableView reloadData];//刷新tableview
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
    
}
#pragma mark --点击cell触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HeadDetailsViewController *headDetailVC = [[HeadDetailsViewController alloc] init];
    
    //    //传下标
    headDetailVC.indexPath = indexPath;
    
    
    HeadLine *headLine = [[HeadLine alloc] init];
    headLine = [self.allHeadLineArray objectAtIndex:indexPath.row];
    
    
    headDetailVC.headLine = headLine;
    
    [self.navigationController pushViewController:headDetailVC animated:YES];
    
    

    
    
    
}


//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return self.allHeadLineArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    return   100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    HeadLineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) {
        cell = [[HeadLineTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    HeadLine *headline = [self.allHeadLineArray objectAtIndex:indexPath.row];

    NSString *urlStr = headline.ImageUrl;
    
    NSString *urlStrEncode = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStrEncode];

    [cell.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"picholder@2x.png"]];
    

    cell.titleLabel.text = headline.title;
    cell.timeLabel.text = headline.date;

        cell.headline = headline;
    
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
